package Map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class demo02_hashmap {
    public static void main(String[] args) {
        m2();
    }

    private static void m2() {
        HashMap<Person,String> map = new HashMap<>();
        /*
        * 在person类里重写equals方法和hashcode方法，
        * 使得map存储自定义对象时不存在同名且同年龄的对象*/

        map.put(new Person("张三",18),"cn");
        map.put(new Person("李四",19),"uk");
        map.put(new Person("王五",20),"us");
        map.put(new Person("张三",18),"jp");

        Set<Map.Entry<Person, String>> entries = map.entrySet();
        for (Map.Entry<Person,String > personStringEntry:
             entries) {
            Person key = personStringEntry.getKey();
            String value = personStringEntry.getValue();
            System.out.println(key + ": " + value);
        }
    }

    private static void m1() {

        HashMap<String,Person> hashMap = new HashMap<>();
        hashMap.put("cn",new Person("张三",18));
        hashMap.put("uk",new Person("李四",19));
        hashMap.put("us",new Person("王五",20));
        hashMap.put("jp",new Person("赵六",90));

        Set<String> stringSet = hashMap.keySet();
        for (String i:
             stringSet) {
            Person p = hashMap.get(i);
            System.out.println(p);
        }


    }
}
